<?php
include_once('config/config.php');

if($_POST)
{
    // 接收表单输入验证码
    $imgcode = $_POST['imgcode'];

    // 接收用户名跟密码
    $username = $_POST['username'];

    $password = $_POST['password'];

    if($_SESSION['imgcode'] != $imgcode)
    {
        ShowMsg('验证码错误');
        exit;
    }

    // 查询数据库是否存在该用户名
    $admin = find("SELECT * FROM `pre_admin` WHERE `username` = '$username'");

    if(!$admin)
    {
        ShowMsg('该用户不存在');
        exit;
    }

    // 获取表单的密码进行加密后的值
    $repass = md5($password . $admin['salt']);

    if($repass != $admin['password'])
    {
        ShowMsg('密码错误');
        exit;
    }

    // 用id + 密码盐 => md5
    $LoginAdmin = md5($admin['id'] . $admin['salt']);

    // 说明登录成功 存一个登录信息
    setcookie('LoginAdmin',$LoginAdmin);
    ShowMsg('登录成功','index.php');
    exit;
}

?>

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>登录页</title>
    <meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="">
    <meta name="author" content="">

    <link rel="stylesheet" type="text/css" href="static/lib/bootstrap/css/bootstrap.css">
    
    <link rel="stylesheet" type="text/css" href="static/stylesheets/theme.css">
    <link rel="stylesheet" href="static/lib/font-awesome/css/font-awesome.css">

    <script src="static/lib/jquery-1.7.2.min.js" type="text/javascript"></script>
  </head>
  
  <body> 
    
    <div class="navbar">
        <div class="navbar-inner">
            <a class="brand" href="index.html"><span class="second">Admin</span></a>
        </div>
    </div>

    <div class="row-fluid">
        <div class="dialog">
            <div class="block">
                <p class="block-heading">登录</p>
                <div class="block-body">
                    <form method="post" enctype="multipart/form-data">
						<!-- 用户名 -->
                        <label>用户名</label>
                        <input type="text" name="username" class="span12" placeholder="请输入用户名" />
						<!-- 密码 -->
						<label>密码</label>
						<input type="password" name="password" class="span12" placeholder="请输入密码" />
                        <label>验证码</label>
                        <div style="display: flex; align-items: center;" >
							<input type="text" name="imgcode" style="margin: 0px;width: 79%;" class="span9" placeholder="请输入验证码">
							<img src="config/imgcode.php" onclick="this.src='config/imgcode.php'" />
						</div>
						<!-- 按钮 -->
						<button type="submit" class="btn btn-primary pull-right" style="margin-top: 20px;">登录</button>
                        <div class="clearfix"></div>
                    </form>
                </div>
            </div>
        </div>
    </div>
  </body>
</html>


